<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TMDT Project Admin</title>
<link rel="stylesheet" href="/admin/resources/css/reset.css"
	type="text/css" media="screen" />
<link rel="stylesheet" href="/admin/resources/css/style.css"
	type="text/css" media="screen" />
<link rel="stylesheet" href="/admin/resources/css/invalid.css"
	type="text/css" media="screen" />
<script type="text/javascript"
	src="/admin/resources/scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript"
	src="/scripts/jquery-1.8.3.min.js"></script>	
<script type="text/javascript"
	src="/admin/resources/scripts/simpla.jquery.configuration.js"></script>
<script type="text/javascript" src="/admin/resources/scripts/facebox.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/jquery.wysiwyg.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/jquery.datePicker.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/jquery.date.js"></script>
	
</head>
<body>
<div id="body-wrapper"><!-- Wrapper for the radial gradient background -->

<div id="sidebar">
<div id="sidebar-wrapper"><!-- Sidebar with logo and menu -->

<h1 id="sidebar-title"><a href="#">TMDT Project Admin</a></h1>

<!--  Logo (221px wide) --> 
	<a href="#"><img id="logo"
	src="/admin/resources/images/logo.png" alt="Simpla Admin logo" /></a>

			
			<?php include_once 'all_product_main_nav.php'?>
			

			
		</div>
</div>
<!-- End #sidebar -->
<div id="clear"></div>
<div id="main-content"><!-- Main Content Section with everything -->
	<?php include_once '../shortcut_buttons.php' ?>
		<?php 
		include_once '../config.php' ?>
<noscript>
<!-- Show a notification if the user has disabled javascript -->
<div class="notification error png_bg">
<div>Javascript is disabled or is not supported by your browser. Please
<a href="http://browsehappy.com/" title="Upgrade to a better browser">upgrade</a>
your browser or <a
	href="http://www.google.com/support/bin/answer.py?answer=23852"
	title="Enable Javascript in your browser">enable</a> Javascript to
navigate the interface properly.</div>
</div>
</noscript>

<?php
		include_once '../config.php';
		$productID="";
		
		$nameErr = $productIDErr = $categoryErr= "";
		$product_name = $product_id = $product_desc= $category = $man_name=$sup_name =$price= $image="";
		$message ="";
		$err= false;
	 
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
	    if (empty($_POST["product_name"])) {
	        $nameErr = "Product name is required.";
			$err=true;

	       
	    }
	    else {
	        $product_name = trim($_POST["product_name"]);
	    }
	 
	    if (empty($_POST["product_id"])) {
	        $productIDErr = "Product ID is required.";
	        $err =true;

	    }
	    else {
	        $product_id = trim($_POST["product_id"]);
	    }

		if (empty($_POST["product_desc"]) || trim(strtoupper($_POST["product_desc"])== "")) {
	        $product_desc = "";
	    }
	    else {
	        $product_desc = $_POST["product_desc"];
	    }
	    
	    if (empty($_POST["category"]))  {
	        $categoryErr = "Category is required.";
	        $err =true;

	    }
	    else {
	        $category = trim($_POST["category"]);
	    }
	    
		if (empty($_POST["price"]) || trim(strtoupper($_POST["price"])== "NULL")) {
	        $price = "NULL";
	    }
	    else {
	        $price = trim($_POST["price"]);
	    }
	    
	    if (empty($_POST["man_name"]) || trim(strtoupper($_POST["man_name"])== "")) {
	        $man_name = "";
	    }
	    else {
	        $man_name = trim($_POST["man_name"]);
	    }

		if (empty($_POST["sup_name"]) || trim(strtoupper($_POST["sup_name"])== "")) {
	        $sup_name = "";
	    }
	    else {
	        $sup_name = trim($_POST["sup_name"]);
	    }
	    
		if (empty($_POST["image"]) || trim(strtoupper($_POST["image"])== "")) {
	        $image = "";
	    }
	    else {
	        $image = trim($_POST["image"]);
	    }	 
	    

			
		//////////////////////////////////////////////////
		//Insert Auction into Database
		//////////////////////////////////////////////////
		if ($err == true) {
			
		?>
				
				<div class="notification error png_bg">
					<a href="#" class="close"><img src="/admin/resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close"/></a>
					<div>
						Validate Error: <?php echo "$nameErr  $productIDErr  $categoryErr"?>	
					</div>
				</div>	

				<?php 			
		}
		else{
	
			
			$connection = mysql_connect ( $host, $username, $password ) or die ( "Could not connect to MySql" . mysql_error () );
			
			mysql_select_db ( $db, $connection ) or die ( "Could not connect to MySql" . mysql_error () );

				$statement = "UPDATE Product SET  name='$product_name',productID = '$product_id',description ='$product_desc',price= $price,manufacturer_name = '$man_name',
				supplier_name = '$sup_name',category_name ='$category',image='$image' where productID = '$product_id'";
				
		
			$result= mysql_query($statement);
			
			if (! $result) {
				$message = " MySQL: Error " . mysql_errno () . " : " . mysql_error ();
		?>
				
				<div class="notification error png_bg">
					<a href="#" class="close"><img src="/admin/resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close"/></a>
					<div>
						<?php echo $statement."  ".$message; ?>	
					</div>
				</div>	

				<?php 
			}
			else {
				$message = "Updatet Product Successful";
				?>
			<div class="notification success png_bg">
				<a href="#" class="close"><img src="/admin/resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
				<div>
					<?php echo $message; ?>	
				</div>
			</div>

				<?php 
			}
			mysql_close($connection);
		}
	}
	else if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['id']) && !empty($_GET['id'])){
			$product_id = $_GET['id'];
		?><!-- 
		 -->
	<div class="content-box-content">

<div class="tab-content current" id="update_auction_tab">

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
	method="post">

<fieldset><!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->
<?php 
			$connection = mysql_connect ( $host, $username, $password ) or die ( "Could not connect to MySql" . mysql_error () );
			mysql_select_db ( $db, $connection ) or die ( "Could not connect to MySql" . mysql_error () );
			$result = @mysql_query ( "select * from Product where productID='$product_id'", $connection );
			$row = @mysql_fetch_assoc ( $result ) ;
				$product_name = $row['name'];
				
				$man_name = $row ['manufacturer_name'];
				$sup_name = $row ['supplier_name'];
				$category_name = $row ['category_name'];
				$price = $row['price'];
				$image= $row['image'];
				

			
?>
<p><label>Product Name(*)</label> <input
	class="text-input medium-input datepicker" type="text"
	id="product_name" name="product_name" value= "<?php echo htmlspecialchars($product_name);?>"/> 
	<span id="auction_name_message" class = "validate_err" ><?php echo $nameErr?></span> <!-- Classes for input-notification: success, error, information, attention -->
<br />
<small>Name for this product</small></p>

<p><label>Product ID(*)</label> <input class="text-input small-input"
	type="text" id="product_id" name="product_id" value= "<?php echo htmlspecialchars($product_id);?>"/>
	 <span id="product_id_message" class = "validate_err"><?php echo $productIDErr?></span>
</p>


								
		<p>
			<label>Description</label>
			<textarea class="text-input textarea wysiwyg" id="product_desc" name="product_desc" cols="79" rows="15">
			<?php echo htmlspecialchars($product_desc);?>
			</textarea>
		</p>
	<label>Category</label>              
	<select class="small-input" name="category">
		<?php 
			$result = mysql_query("select name from Category");
			
			while ($row = mysql_fetch_array($result,MYSQL_NUM)){
				
		?>
		<option value=<?php echo htmlspecialchars("'".$row[0]."'")?>><?php echo htmlspecialchars($row[0])?></option>
		<?php 
			}
			mysql_close($connection);
		?>
	</select> 
	<span id="category_message" class = "validate_err"><?php echo $categoryErr?></span>


<p><label>Price</label> 
<input class="text-input small-input"
	type="text" id="price" name="price" value = "<?php echo htmlspecialchars($price);?>"/> 
	<span id="price_message"></span> <!-- Classes for input-notification: success, error, information, attention -->
<br />
<small>Price of this product</small></p>

<p><label>Manufacturer Name</label> 
<input class="text-input small-input"
	type="text" id="man_name" name="man_name" value= "<?php echo htmlspecialchars($man_name);?>"/>
	 <span id="man_name_message"></span> <br />
</p>


<p><label>Supplier Name</label>
 <input class="text-input small-input"
	type="text" id="sup_name" name="sup_name" value= "<?php echo htmlspecialchars($sup_name);?>"/> 
	<span id="sup_name_message" class = "validate_err"></span> <br />
</p>


<p><label>Image</label> <input class="text-input small-input"
	type="text" id="image" name="image" value= "<?php echo htmlspecialchars($image);?>"/> 
	<span id="$image_message"></span> <!-- Classes for input-notification: success, error, information, attention -->
<br />
<small>Path to the product image. For example: ./images/iphone.png</small></p>


<input name='id' value = '<?php echo htmlspecialchars($product_id); ?> '/>
<p><input class="button" type="submit" value="Update" /> <a
	href="/admin/pages/allproducts.php"><input class="button" value="Cancel" /> </a></p>

</fieldset>

<div class="clear"></div>
<!-- End .clear -->

</form>

</div>
<!-- End #tab2 --></div>
<!-- End .content-box-content --></div>
<!-- End .content-box -->


<div class="clear"></div>
		<?php }
		else{
			header("Location: /admin/allproducts.php");
		};
		
	?>
